﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_PERMISSION.Base;
using ERP_RORZE_PERMISSION.Base.Objects;
using ERP_RORZE_PERMISSION.UserControls;

namespace ERP_RORZE_PERMISSION.Forms
{
    public partial class frm_UserEdit : DevExpress.XtraEditors.XtraForm
    {
        public string UserName;
        public int UserGroupID;
        public int DepartmentID;
        public string StaffID;
        public string Password;
        private ucUser parent;
        private DataTable dtStaff;
        private string pwDefault = "1234567890";
        private LogUtils log;

        public frm_UserEdit(ucUser fUser)
        {
            InitializeComponent();
            parent = fUser;
            this.CancelButton = this.btCancel;
            log = new LogUtils();
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            Users user = new Users();
            if (GetData(user))
            {
                string update = "UPDATE [222_Users] SET [UserName] = N'" + user.UserName +
                                "',[Password] = N'" + user.Password +
                                "',[UserGroupID] = " + user.UserGroupID +
                                ",[DepartmentID] = " + user.DepartmentID +
                                ",[StaffID] =N'" + user.StaffID + "'  WHERE [UserName]= N'" + UserName + "'";

                if (BaseDAO.ExecuteNonQuery(update) > 0)
                {

                    //write Log
                    log.Table = LogConstant.Table.SystemAdmin.Users;
                    log.Task = LogConstant.Task.Update;
                    log.Description = "UserName: " + user.UserName + "; UserGroupID: " + user.UserGroupID + "; StaffID: " + user.StaffID;
                    log.WriteLog();

                    if (!Utilities.MessageConfirmationYesNo(PermissionConstants.Message.UpdateSuccessAndQuestion))
                    {
                        this.Close();
                    }
                }
                else
                {

                    Utilities.MessageErrorOk(PermissionConstants.Message.UpdateFailed);
                }
                parent.BindDataGrid();
            }
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frm_UserEdit_Load(object sender, EventArgs e)
        {
            BindDataUserGroup();
            BindDataStaff();
            BindData();

        }

        protected override bool ProcessKeyPreview(ref Message m)
        {
            if (m.Msg == 0x0100 && (int)m.WParam == 13)
            {
                this.ProcessTabKey(true);
            }
            return base.ProcessKeyPreview(ref m);
        }

        #region Private methods


        private bool GetData(Users o)
        {
            #region Check valid
            if (Utilities.IsNullOrEmpty(txtUserName.Text.Trim()))
            {
                MessageBox.Show("UserName can not be empty.", PermissionConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(txtPassword.Text.Trim()))
            {
                MessageBox.Show("Pasword can not be empty.", PermissionConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (!txtRetypeNew.Text.Trim().Equals(txtPassword.Text.Trim()))
            {
                MessageBox.Show("You must enter the same password twice in order to confirm it.", PermissionConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtRetypeNew.Focus();
                return false;
            }

            if (Utilities.IsNullOrEmpty(cmbUserGroup.SelectedValue) || cmbUserGroup.SelectedItem == cmbUserGroup.Items[0])
            {
                MessageBox.Show("Please select UserGroup.", PermissionConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(cmbStaffID.SelectedValue) || cmbStaffID.SelectedItem == cmbStaffID.Items[0])
            {
                MessageBox.Show("Please select Staff Code.", PermissionConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            //if (CheckDuplicate(txtUserName.Text.Trim()))
            //{
            //    MessageBox.Show("Username already exists.", PermissionConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    return false;
            //}

            #endregion

            o.UserName = txtUserName.Text;
            if (!txtPassword.Text.Equals(pwDefault))
            {
                o.Password = Utilities.EncryptPassword(txtPassword.Text, PermissionConstants.Key);
            }
            else
            {
                o.Password = Password;
            }
            o.UserGroupID = int.Parse(cmbUserGroup.SelectedValue.ToString());
            o.StaffID = cmbStaffID.SelectedValue.ToString();

            return true;
        }

        private void BindDataUserGroup()
        {
            DataTable dt = BaseDAO.ExecuteQuery("Select [GroupID], [GroupName] from [222_UserGroup] order by [GroupName]");
            DataRow row = dt.NewRow();

            row["GroupName"] = "";
            row["GroupID"] = DBNull.Value;
            dt.Rows.InsertAt(row, 0);

            cmbUserGroup.DataSource = dt;
            cmbUserGroup.DisplayMember = "GroupName";
            cmbUserGroup.ValueMember = "GroupID";
            //cmbUserGroup.SelectedItem = cmbUserGroup.Items[0];

        }

        private void BindDataStaff()
        {
            dtStaff = BaseDAO.ExecuteQuery("Select [StaffID], [StaffName] from [222_Staff]");
            DataRow row = dtStaff.NewRow();

            row["StaffID"] = "";
            row["StaffName"] = "";
            dtStaff.Rows.InsertAt(row, 0);

            cmbStaffID.DataSource = dtStaff;
            cmbStaffID.DisplayMember = "StaffID";
            cmbStaffID.ValueMember = "StaffID";
            //cmbStaffID.SelectedItem = cmbStaffID.Items[0];

        }

        private void BindData()
        {
            txtUserName.Text = UserName;
            txtPassword.Text = pwDefault;
            txtRetypeNew.Text = pwDefault;
            cmbUserGroup.SelectedValue = UserGroupID;
            cmbDepartment.SelectedValue = DepartmentID;
            cmbStaffID.SelectedValue = StaffID;
        }

        #endregion


    }
}